package cn.itcast.order.service;

import cn.itcast.feign.client.ItemClient;
import cn.itcast.feign.client.UserClient;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.hmall.pojo.order.Order;
import cn.itcast.hmall.pojo.order.OrderDetail;
import cn.itcast.hmall.pojo.order.OrderLogistics;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.utils.ThreadLocalUtil;
import cn.itcast.order.mapper.OrderDetailMapper;
import cn.itcast.order.mapper.OrderLogicMapper;
import cn.itcast.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;
@Service
public class orderService {
    @Autowired
    private OrderLogicMapper orderLogicMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper odm;
    public String order1(Map<String, Long> map){
        System.out.println(map);


        //根据雪花算法生成订单id**(mybatisplus自定生成)
        //商品微服务提供FeignClient，实现根据id查询商品的接口
        Long itemId = map.get("itemId");
        //根据itemId查询商品信息
        Item byId = itemClient.getById(itemId);
        //基于商品价格、购买数量计算商品总价：totalFee
        Long price = byId.getPrice();
        String num = "num";
        Long aLong = map.get(num);
        Long totalFee = price * aLong;
        //封装Order对象，初始status为  未支付
        Order order = new Order();
        order.setStatus(1);
        order.setTotalFee(totalFee);
        Long userId = ThreadLocalUtil.getUserId();
        order.setUserId(userId);
        Long paymentType = map.get("paymentType");
        String s = paymentType.toString();
        order.setPaymentType(Integer.parseInt(s));
        //写入数据库tb_order表中
        int insert = orderMapper.insert(order);

        //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        odm.insert(orderDetail);
        //将user微服务的`根据id查询地址`接口封装为FeignClient
        //.....
        //根据addressId查询user-service服务，获取地址信息
        Long addressId = map.get("addressId");
        Address add = userClient.add(addressId.toString());
        //将地址封装为`OrderLogistics`对象，写入tb_order_logistics表(物流信息表)
        OrderLogistics or = new OrderLogistics();
        or.setOrderId(order.getId());
        System.err.println("order的id" + order.getId());
        or.setContact(add.getContact());
        or.setCity(add.getCity());
        or.setMobile(add.getMobile());
        or.setProvince(add.getProvince());
        or.setStreet(add.getStreet());
        or.setTown(add.getTown());

        orderLogicMapper.insert(or);
        //在item商品微服务提供减库存接口，并编写FeignClient
        //调用item微服务的减库存接口

        System.out.println(order.getId());
        Boolean decrease = itemClient.decrease(itemId.toString());


        return order.toString();
    }
}
